#load 3 datasets: GSS.dta, anespanel1-13-12REDUCED.sav, and #anes2008_2009paneloffwave5subset.sav from dataverse

anes<- merge(anespanel1-13-12REDUCED.sav,anes2008_2009paneloffwave5subset.sav,by="caseid")
names(anes)

rnum<-anes$rnumcall
summary(rnum)
rnum[rnum>15]<-15
table(rnum)

days<-anes$daystorespond
summary(days)
days[days>21]<-21
table(days)

anesalone<-c()
anesalone[anes$cpq5=='1']<-1
anesalone[anes$cpq5=='2']<-0
anesalone[anes$cpq5=='3']<-0
anesalone[anes$cpq5=='4']<-0
anesalone[anes$cpq5=='5']<-0
anesalone[anes$cpq5=='6']<-0
anesalone[anes$cpq5=='7']<-0
anesalone[anes$cpq5=='8']<-0
anesalone[anes$cpq5=='9']<-0
anesalone[anes$cpq5=='11']<-0
anesalone[anes$cpq5=='15']<-0

aneswebtv<-c()
aneswebtv[anes$webtv=='1. R required MSN TV device']<-1
aneswebtv[anes$webtv=='0. R has a PC']<-0

summary(anes$cps_voice)
anesvoice<-c()
anesvoice[anes$cps_voice=='Yes']<-1
anesvoice[anes$cps_voice=='No']<-0
anesvoice[is.na(anesvoice)]<-0
summary(anesvoice)

anesfun<-c()
anesfun[anes$cps_fun=='Yes']<-1
anesfun[anes$cps_fun=='No']<-0
anesfun[is.na(anesfun)]<-0
summary(anesfun)

anesedu<-c()
anesedu[anes$cps_educational=='Yes']<-1
anesedu[anes$cps_educational=='No']<-0
anesedu[is.na(anesedu)]<-0
summary(anesedu)
table(anesedu)

anesintersting<-c()
anesintersting[anes$cps_interesting=='Yes']<-1
anesintersting[anes$cps_interesting=='No']<-0
anesintersting[is.na(anesintersting)]<-0
summary(anesintersting)
table(anesintersting)

anesmoney<-c()
anesmoney[anes$cps_money=='No']<-0
anesmoney[anes$cps_money=='Yes']<-1
anesmoney[is.na(anesmoney)]<-0
summary(anesmoney)
table(anesmoney)

anesmotivwebtv<-c()
anesmotivwebtv[anes$cps_webtv=='No']<-0
anesmotivwebtv[anes$cps_webtv=='Yes']<-1
anesmotivwebtv[is.na(anesmotivwebtv)]<-0
summary(anesmotivwebtv)
table(anesmotivwebtv)

anesinternet<-c()
anesinternet[anes$cps_internet=='No']<-0
anesinternet[anes$cps_internet=='Yes']<-1
anesinternet[is.na(anesinternet)]<-0
summary(anesinternet)
table(anesinternet)

anesmoneyonly<-c()
anesmoneyonly[anesvoice=='0' & anesfun=='0' & anesedu=='0' & anesintersting=='0' & anesmoney=='1' & anesinternet=='0' & anesmotivwebtv=='0']<-1
anesmoneyonly[is.na(anesmoneyonly)]<-0
table(anesmoneyonly)

table(anes$MOTIVATION1)
nomotiv1<-c()
nomotiv1[anes$MOTIVATION1=='No response']<-1
nomotiv1[anes$MOTIVATION1=='N/A']<-1
nomotiv1[anes$MOTIVATION1=='No/no other reason/reasons already covered']<-1
nomotiv1[is.na(nomotiv1)]<-0
summary(nomotiv1)

nonmotiv<-c()
nonmotiv[anes$cps_none=='Yes' & nomotiv1=='1']<-1
nonmotiv[is.na(nonmotiv)]<-0
summary(nonmotiv)

table(anes$oesvyexpNEG_W1)
negexpw1<-c()
negexpw1[anes$oesvyexpNEG_W1=='Negative comment']<-1
negexpw1[anes$oesvyexpNEG_W1=='No negative comment']<-0
table(negexpw1)

posexpw1<-c()
posexpw1[anes$oesvyexpPOS_W1=='Positive comment']<-1
posexpw1[anes$oesvyexpPOS_W1=='No positive comment']<-0
table(posexpw1)


noexpw1<-c()
noexpw1[anes$oesvyNONE_W1=='No comment']<-1
noexpw1[anes$oesvyNONE_W1=='Comment given']<-0
table(noexpw1)

summary(anes$w1k1)
polinterest<-c()
polinterest[anes$w1k1=='1. Extremely interested']<-5
polinterest[anes$w1k1=='2. Very interested']<-4
polinterest[anes$w1k1=='3. Moderately interested']<-3
polinterest[anes$w1k1=='4. Slightly interested']<-2
polinterest[anes$w1k1=='5. Not interested at all']<-1
table(polinterest)


aneswoman<-c()
aneswoman[anes$der01=='2. Female']<-1
aneswoman[anes$der01=='1. Male']<-0
table(anes$der01)
table(aneswoman)


table(anes$der04)
anesnonwhite<-c()
anesnonwhite[anes$der04=='2. Black, non-Hispanic']<-1
anesnonwhite[anes$der04=='3. Hispanic']<-1
anesnonwhite[anes$der04=='4. Other, non-Hispanic']<-1
anesnonwhite[anes$der04=='1. White, non-Hispanic']<-0
table(anesnonwhite)

aneseduc<-c()
aneseduc[anes$der05=='1. No high school diploma']<-1
aneseduc[anes$der05=='2. High school diploma']<-2
aneseduc[anes$der05=="3. Some college, no bachelor's degree"]<-3
aneseduc[anes$der05=="4. Bachelor's degree"]<-4
aneseduc[anes$der05=='5. Graduate degree']<-5
table(aneseduc)

anesincome<-c()
anesincome[anes$der06=='1. less than $5,000']<-1
anesincome[anes$der06=='2. $5,000  to $7,499']<-2
anesincome[anes$der06=='3. $7,500  to $9,999']<-3
anesincome[anes$der06=='4. $10,000  to $12,499']<-4
anesincome[anes$der06=='5. $12,500  to $14,999']<-5
anesincome[anes$der06=='6. $15,000  to $19,999']<-6
anesincome[anes$der06=='7. $20,000  to $24,999']<-7
anesincome[anes$der06=='8. $25,000  to $29,999']<-8
anesincome[anes$der06=='9. $30,000  to $34,999']<-9
anesincome[anes$der06=='10. $35,000  to $39,999']<-10
anesincome[anes$der06=='11. $40,000  to $49,999']<-11
anesincome[anes$der06=='12. $50,000  to $59,999']<-12
anesincome[anes$der06=='13. $60,000  to $74,999']<-13
anesincome[anes$der06=='14. $75,000  to $84,999']<-14
anesincome[anes$der06=='15. $85,000  to $99,999']<-15
anesincome[anes$der06=='16. $100,000  to $124,999']<-16
anesincome[anes$der06=='17. $125,000  to $149,999']<-17
anesincome[anes$der06=='18. $150,000  to $174,999']<-18
anesincome[anes$der06=='19. $175,000  or more']<-18

table(anes$der07)
aneshome<-c()
aneshome[anes$der07=='1. R owns home']<-1
aneshome[anes$der07=='2. R rents home']<-0
aneshome[anes$der07=='3. Other arrangement']<-0
table(aneshome)

table(anes$cpq17)
anesemp<-c()
anesemp[anes$cpq17=='1. Working - as a paid employee']<-1
anesemp[anes$cpq17=='2. Working - self-employed']<-1
anesemp[anes$cpq17=='3. Not working - on temporary layoff from a job']<-0
anesemp[anes$cpq17=='4. Not working - looking for work']<-0
anesemp[anes$cpq17=='5. Not working - retired']<-0
anesemp[anes$cpq17=='6. Not working - disabled']<-0
anesemp[anes$cpq17=='7. Not working - other']<-0
table(anesemp)

table(anes$webtv)
aneswebtv<-c()
aneswebtv[anes$webtv=='1. R required MSN TV device']<-1
aneswebtv[anes$webtv=='0. R has a PC']<-0
table(aneswebtv)



anesotherlanguage<-c()
anesotherlanguage[anes$cpq14a=='1. Yes']<-1
anesotherlanguage[anes$cpq14a=='2. No']<-0


anesweights<-anes$wgtcs01

summary(anes$cpmember2_age)


child1<-c()
child1[anes$cpmember2_age=='0']<-1
child1[anes$cpmember2_age=='1']<-1
child1[anes$cpmember2_age=='2']<-1
child1[anes$cpmember2_age=='3']<-1
child1[anes$cpmember2_age=='4']<-1
child1[anes$cpmember2_age=='5']<-1
child1[is.na(child1)]<-0
summary(child1)

child2<-c()
child2[anes$cpmember3_age=='0']<-1
child2[anes$cpmember3_age=='1']<-1
child2[anes$cpmember3_age=='2']<-1
child2[anes$cpmember3_age=='3']<-1
child2[anes$cpmember3_age=='4']<-1
child2[anes$cpmember3_age=='5']<-1
child2[is.na(child2)]<-0
summary(child2)

child3<-c()
child3[anes$cpmember4_age=='0']<-1
child3[anes$cpmember4_age=='1']<-1
child3[anes$cpmember4_age=='2']<-1
child3[anes$cpmember4_age=='3']<-1
child3[anes$cpmember4_age=='4']<-1
child3[anes$cpmember4_age=='5']<-1
child3[is.na(child3)]<-0
summary(child3)

child4<-c()
child4[anes$cpmember5_age=='0']<-1
child4[anes$cpmember5_age=='1']<-1
child4[anes$cpmember5_age=='2']<-1
child4[anes$cpmember5_age=='3']<-1
child4[anes$cpmember5_age=='4']<-1
child4[anes$cpmember5_age=='5']<-1
child4[is.na(child4)]<-0
summary(child4)

child5<-c()
child5[anes$cpmember6_age=='0']<-1
child5[anes$cpmember6_age=='1']<-1
child5[anes$cpmember6_age=='2']<-1
child5[anes$cpmember6_age=='3']<-1
child5[anes$cpmember6_age=='4']<-1
child5[anes$cpmember6_age=='5']<-1
child5[is.na(child5)]<-0
summary(child5)

child6<-c()
child6[anes$cpmember7_age=='0']<-1
child6[anes$cpmember7_age=='1']<-1
child6[anes$cpmember7_age=='2']<-1
child6[anes$cpmember7_age=='3']<-1
child6[anes$cpmember7_age=='4']<-1
child6[anes$cpmember7_age=='5']<-1
child6[is.na(child6)]<-0
summary(child6)

child7<-c()
child7[anes$cpmember8_age=='0']<-1
child7[anes$cpmember8_age=='1']<-1
child7[anes$cpmember8_age=='2']<-1
child7[anes$cpmember8_age=='3']<-1
child7[anes$cpmember8_age=='4']<-1
child7[anes$cpmember8_age=='5']<-1
child7[is.na(child7)]<-0
summary(child7)

child8<-c()
child8[anes$cpmember9_age=='0']<-1
child8[anes$cpmember9_age=='1']<-1
child8[anes$cpmember9_age=='2']<-1
child8[anes$cpmember9_age=='3']<-1
child8[anes$cpmember9_age=='4']<-1
child8[anes$cpmember9_age=='5']<-1
child8[is.na(child8)]<-0
summary(child8)

children<-(child1+child2+child3+child4+child5+child6+child7+child8)
summary(children)


summary(anes$w1b1)
anesrefusals1<-c()
anesrefusals1[anes$w1b1=='-7. No answer'|anes$w1b1=='-9. Refused']<-1
anesrefusals1[is.na(anesrefusals1)]<-0
table(anesrefusals1)


table(anes$w1m1)
anesrefusals2<-c()
anesrefusals2[anes$w1m1=='-7. No answer'|anes$w1m1=='-9. Refused']<-1
anesrefusals2[is.na(anesrefusals2)]<-0
table(anesrefusals2)

table(anes$w1n1)
anesrefusals3<-c()
anesrefusals3[anes$w1n1=='-7. No answer'|anes$w1n1=='-9. Refused']<-1
anesrefusals3[is.na(anesrefusals3)]<-0
table(anesrefusals3)

table(anes$w1a6)
anesrefusals4<-c()
anesrefusals4[anes$w1a6=='-7. No answer'|anes$w1a6=='-9. Refused']<-1
anesrefusals4[is.na(anesrefusals4)]<-0
table(anesrefusals4)

table(anes$w1n4)
anesrefusals5<-c()
anesrefusals5[anes$w1n4=='-7. No answer'|anes$w1n4=='-9. Refused']<-1
anesrefusals5[is.na(anesrefusals5)]<-0
table(anesrefusals5)

table(anes$w1m6)
anesrefusals6<-c()
anesrefusals6[anes$w1m6=='-7. No answer'|anes$w1m6=='-9. Refused']<-1
anesrefusals6[is.na(anesrefusals6)]<-0
table(anesrefusals6)

anesrefusals<-(anesrefusals1+anesrefusals2+anesrefusals3+anesrefusals4+anesrefusals5+anesrefusals6)
summary(anesrefusals)
anesrefusals[anesrefusals>0]<-1
table(anesrefusals)

anesrefuse<-c()
anesrefuse[anes$w1e11 =='-7. No answer'|anes$w1e14 =='-7. No answer'|anes$w1e17 =='-7. No answer'|anes$w1e2 =='-7. No answer'|anes$w1e20 =='-7. No answer'|anes$w1e23 =='-7. No answer'|anes$w1e26 =='-7. No answer'|anes$w1e29 =='-7. No answer'|anes$w1e32 =='-7. No answer'|anes$w1e35 =='-7. No answer'|anes$w1e38 =='-7. No answer'|anes$w1e41 =='-7. No answer'|anes$w1e44 =='-7. No answer'|anes$w1e47 =='-7. No answer'|anes$w1e5 =='-7. No answer'|anes$w1e50 =='-7. No answer'|anes$w1e53 =='-7. No answer'|anes$w1e56 =='-7. No answer'|anes$w1e59 =='-7. No answer'|anes$w1e8=='-7. No answer']<-1
anesrefuse[anesrefusals>0]<-1
anesrefuse[is.na(anesrefuse)]<-0
table(anesrefuse)


anesw5attrit<-c()
anesw5attrit[anes$w1flag=='1. R completed Wave 1'& anes$w5flag=='1. R completed Wave 5']<-0
anesw5attrit[anes$w1flag=='1. R completed Wave 1' & anes$w5flag=='0. R did not complete Wave 5']<-1
summary(anesw5attrit)

anes$aneswoman1<-aneswoman
anes$aneseduc1<-aneseduc
anes$anesincome1<-anesincome
anes$aneshome1<-aneshome
anes$anesemp1<-anesemp
anes$children1<-children
anes$anesotherlanguage1<-anesotherlanguage
anes$anesalone1<-anesalone
anes$aneswebtv1<-aneswebtv
anes$anesweights1<-anesweights
anes$days1<-days
anes$noexpw11<-noexpw1
anes$posexpw11<-posexpw1
anes$negexpw11<-negexpw1
anes$nonmotiv1<-nonmotiv
anes$polinterest1<-polinterest
anes$rnum1<-rnum
anes$moneyonly1<-anesmoneyonly
anes$nonwhite<-anesnonwhite
anes$anesrefusals<-anesrefusals
anes$anesrefuse<-anesrefuse
anes$anesw5attrit1<-anesw5attrit

anes<-data.frame(anes)
anes4<-subset(anes, anes$anesweights1!=0 & anes$aneseduc1!='NA' & anes$aneswoman1!='NA' & anes$anesincome1!='NA' & anes$aneshome1!='NA' & anes$anesemp1!='NA' & anes$anesotherlanguage1!='NA' & anes$anesalone!='NA' & anes$polinterest1!='NA' & anes$aneswebtv1!='NA'& anes$moneyonly1!='NA' & anes$nonmotiv1!='NA' & anes$days1!='NA' & anes$noexpw11!='NA' & anes$negexpw11!='NA' & anes$rnum1!='NA' & anes$nonwhite!='NA' & anes$anesrefuse!='NA')

library(Zelig)

w5attrit2<-zelig(anesw5attrit1~der02+aneswoman1+nonwhite+aneseduc1+anesincome1+aneshome1+anesemp1+children1+anesotherlanguage1+anesalone1, weights=anes4$anesweights1, model="logit", data=anes4)
summary(w5attrit2)
library(apsrtable)
apsrtable(w5attrit2)

w5attrit1<-zelig(anesw5attrit1~der02+aneswoman1+nonwhite+aneseduc1+anesincome1+aneshome1+anesemp1+children1+anesotherlanguage1+anesalone1+polinterest1+aneswebtv1+moneyonly1+nonmotiv1+days1+noexpw11+negexpw11+rnum1+anesrefuse, weights=anes4$anesweights1, model="logit", data=anes4)
summary(w5attrit1)
apsrtable(w5attrit1)

First Diffs

######age
quantile(anes$der02, .05, na.rm = TRUE)
quantile(anes$der02, .95, na.rm = TRUE)
x.high<- setx(w5attrit1, der02=78)
x.low <- setx(w5attrit1, der02=24)
exp.out1<- sim(w5attrit1, x1 = x.high, x = x.low) 
summary(exp.out1)

######Female

x.high<- setx(w5attrit1, aneswoman1=1) 
x.low <- setx(w5attrit1, aneswoman1=0)
exp.out2<- sim(w5attrit1, x1 = x.high, x = x.low) 
summary(exp.out2)


######nonwhite

x.high<- setx(w5attrit1, nonwhite=1) 
x.low <- setx(w5attrit1, nonwhite=0)
exp.out3<- sim(w5attrit1, x1 = x.high, x = x.low) 
summary(exp.out3)


######education
quantile(anes$aneseduc1, .05, na.rm = TRUE)
quantile(anes$aneseduc1, .95, na.rm = TRUE)
x.high<- setx(w5attrit1, aneseduc1=5) 
x.low <- setx(w5attrit1, aneseduc1=2)
exp.out4<- sim(w5attrit1, x1 = x.high, x = x.low) 
summary(exp.out4)

######income 
quantile(anes$anesincome1, .05, na.rm = TRUE)
quantile(anes$anesincome1, .95, na.rm = TRUE)
x.high<- setx(w5attrit1, anesincome1=18) 
x.low <- setx(w5attrit1, anesincome1=4.3)
exp.out5<- sim(w5attrit1, x1 = x.high, x = x.low)  
summary(exp.out5)


#####home ownership

x.high<- setx(w5attrit1, aneshome1= 1) 
x.low <- setx(w5attrit1, aneshome1= 0)
exp.out6 <- sim(w5attrit1, x1 = x.high, x = x.low) 
summary(exp.out6)


#####employed
x.high<- setx(w5attrit1, anesemp1= 1) 
x.low <- setx(w5attrit1, anesemp1= 0)
exp.out7 <- sim(w5attrit1, x1 = x.high, x = x.low) 
summary(exp.out7)

######children at 0 and 1
quantile(anes$children1, .05, na.rm = TRUE)
quantile(anes$children1, .95, na.rm = TRUE)
x.high<- setx(w5attrit1, children1=1) 
x.low <- setx(w5attrit1, children1=0)
exp.out8<- sim(w5attrit1, x1 = x.high, x = x.low)  
summary(exp.out8)

###nonEnglish
x.high<- setx(w5attrit1, anesotherlanguage1=1) 
x.low <- setx(w5attrit1, anesotherlanguage1=0)
exp.out9<- sim(w5attrit1, x1 = x.high, x = x.low) 
summary(exp.out9)

##Alone
x.high<- setx(w5attrit1, anesalone1=1) 
x.low <- setx(w5attrit1, anesalone1=0)
exp.out10<- sim(w5attrit1, x1 = x.high, x = x.low)  
summary(exp.out10)


######polinterest
quantile(anes$polinterest1, .05, na.rm = TRUE)
quantile(anes$polinterest1, .95, na.rm = TRUE)
x.high<- setx(w5attrit1, polinterest1=5) 
x.low <- setx(w5attrit1, polinterest1=2)
exp.out11 <- sim(w5attrit1, x1 = x.high, x = x.low)  
summary(exp.out11)

#####requiring webtv

x.high<- setx(w5attrit1, aneswebtv1 = 1) 
x.low <- setx(w5attrit1, aneswebtv1 = 0)
exp.out12 <- sim(w5attrit1, x1 = x.high, x = x.low)  
summary(exp.out12)

######money only

x.high<- setx(w5attrit1, moneyonly1=1) 
x.low <- setx(w5attrit1, moneyonly1=0)
exp.out13 <- sim(w5attrit1, x1 = x.high, x = x.low)  
summary(exp.out13)


######no motivation

x.high<- setx(w5attrit1, nonmotiv1=1) 
x.low <- setx(w5attrit1, nonmotiv1=0)
exp.out135 <- sim(w5attrit1, x1 = x.high, x = x.low)  
summary(exp.out135)


######negative experience

x.high<- setx(w5attrit1, negexpw11=1) 
x.low <- setx(w5attrit1, negexpw11=0)
exp.out14 <- sim(w5attrit1, x1 = x.high, x = x.low)  
summary(exp.out14)


######no experience

x.high<- setx(w5attrit1, noexpw11=1) 
x.low <- setx(w5attrit1, noexpw11=0)
exp.out145 <- sim(w5attrit1, x1 = x.high, x = x.low)  
summary(exp.out145)

######days to complete
quantile(anes$days1, .05, na.rm = TRUE)
quantile(anes$days1, .95, na.rm = TRUE)
x.high<- setx(w5attrit1, days1=21) 
x.low <- setx(w5attrit1, days1=0)
exp.out155<- sim(w5attrit1, x1 = x.high, x = x.low) 
summary(exp.out155)

######call attempts
quantile(anes$rnum1, .05, na.rm = TRUE)
quantile(anes$rnum1, .95, na.rm = TRUE)
x.high<- setx(w5attrit1, rnum1=15) 
x.low <- setx(w5attrit1, rnum1=1)
exp.out15<- sim(w5attrit1, x1 = x.high, x = x.low) 
summary(exp.out15)


######refusals

x.high<- setx(w5attrit1, anesrefuse=1) 
x.low <- setx(w5attrit1, anesrefuse=0)
exp.out16<- sim(w5attrit1, x1 = x.high, x = x.low) 
summary(exp.out16)

####DESCRIPTIVES -- mean comparisons

library(gtools)
library(gdata)
library(gmodels)

CrossTable(anes4$aneseduc1,anes4$anesw5attrit1)

summary(anes4$aneseduc1[anes4$anesw5attrit1==0])
summary(anes4$aneseduc1[anes4$anesw5attrit1==1])

summary(anes4$aneswoman1[anes4$anesw5attrit1==0])
summary(anes4$aneswoman1[anes4$anesw5attrit1==1])

summary(anes4$nonwhite[anes4$anesw5attrit1==0])
summary(anes4$nonwhite[anes4$anesw5attrit1==1])

summary(anes4$anesincome1[anes4$anesw5attrit1==0])
summary(anes4$anesincome1[anes4$anesw5attrit1==1])

summary(anes4$aneshome1[anes4$anesw5attrit1==0])
summary(anes4$aneshome1[anes4$anesw5attrit1==1])

summary(anes4$anesemp1[anes4$anesw5attrit1==0])
summary(anes4$anesemp1[anes4$anesw5attrit1==1])

summary(anes4$children1[anes4$anesw5attrit1==0])
summary(anes4$children1[anes4$anesw5attrit1==1])

summary(anes4$anesotherlanguage1[anes4$anesw5attrit1==0])
summary(anes4$anesotherlanguage1[anes4$anesw5attrit1==1])

summary(anes4$anesalone1[anes4$anesw5attrit1==0])
summary(anes4$anesalone1[anes4$anesw5attrit1==1])

summary(anes4$polinterest1[anes4$anesw5attrit1==0])
summary(anes4$polinterest1[anes4$anesw5attrit1==1])

summary(anes4$aneswebtv1[anes4$anesw5attrit1==0])
summary(anes4$aneswebtv1[anes4$anesw5attrit1==1])

summary(anes4$moneyonly1[anes4$anesw5attrit1==0])
summary(anes4$moneyonly1[anes4$anesw5attrit1==1])

summary(anes4$nonmotiv1[anes4$anesw5attrit1==0])
summary(anes4$nonmotiv1[anes4$anesw5attrit1==1])

summary(anes4$days1[anes4$anesw5attrit1==0])
summary(anes4$days1[anes4$anesw5attrit1==1])

summary(anes4$noexpw11[anes4$anesw5attrit1==0])
summary(anes4$noexpw11[anes4$anesw5attrit1==1])

summary(anes4$negexpw11[anes4$anesw5attrit1==0])
summary(anes4$negexpw11[anes4$anesw5attrit1==1])

summary(anes4$rnum1[anes4$anesw5attrit1==0])
summary(anes4$rnum1[anes4$anesw5attrit1==1])

summary(anes4$der02[anes4$anesw5attrit1==0])
summary(anes4$der02[anes4$anesw5attrit1==1])

summary(anes4$anesrefuse[anes4$anesw5attrit1==0])
summary(anes4$anesrefuse[anes4$anesw5attrit1==1])

###Imputation

####new dependent variables for imputation: 
table(anes$W5Q1_el11_GV)
primint<-c()
primint[anes$W5Q1_el11_GV=='Moderatelyinterested']<-0
primint[anes$W5Q1_el11_GV=='Not at allinterested']<-0
primint[anes$W5Q1_el11_GV=='Veryinterested']<-1
summary(primint)
table(primint)

table(anes$W5Q3)
papnews2<-c()
papnews2[anes$W5Q3=='Every day']<-1
papnews2[anes$W5Q3=='A few times a week']<-0
papnews2[anes$W5Q3=='Once a week']<-0
papnews2[anes$W5Q3=='Less than once a week']<-0
papnews2[anes$W5Q3=='Never']<-0
summary(papnews2)
table(papnews2)


table(anes$W5Q37)
global<-c()
global[anes$W5Q37=='Very concerned']<-1
global[anes$W5Q37=='Concerned']<-0
global[anes$W5Q37=='Largely unconcerned']<-0
global[anes$W5Q37=='Mildly concerned']<-0
global[anes$W5Q37=='Totally unconcerned']<-0
global[anes$W5Q37=='No Answer']<-NA
table(global)

table(anes$W5Q53_el5_GV28)
intuse<-anes$W5Q53_el5_GV28
summary(intuse)
intuse[intuse<0]<-NA
intuse[intuse>30]<-30
quantile(intuse, .95, na.rm = TRUE)
summary(intuse)


global[anesw5attrit=='1']<-99
primint[anesw5attrit=='1']<-99
papnews2[anesw5attrit=='1']<-99
intuse[anesw5attrit=='1']<-99

anes$global<-global
anes$intuse<-intuse
anes$papnews2<-papnews2
anes$primint<-primint

primintdata<-data.frame(age=anes$der02, female=anes$aneswoman1, race=anes$nonwhite, educ=anes$aneseduc1, income=anes$anesincome1, home=anes$aneshome1, emp=anes$anesemp1, child=anes$children1, lang=anes$anesotherlanguage1, alone=anes$anesalone1, polint=anes$polinterest1, webtv=anes$aneswebtv1, money=anes$moneyonly1, nomotiv=anes$nonmotiv1, days=anes$days1, noexp=anes$noexpw1, negexp=anes$negexpw1, rnum=anes$rnum1, anesrefuse=anes$anesrefuse, primint=anes$primint)

primintdata<-subset(primintdata, primintdata$age!='NA'& primintdata$female!='NA'& primintdata$race!='NA'& primintdata$educ!='NA'& primintdata$income!='NA'& primintdata$home!='NA'& primintdata$emp!='NA'& primintdata$child!='NA'& primintdata$lang!='NA'& primintdata$alone!='NA'& primintdata$polint!='NA'& primintdata$webtv!='NA'& primintdata$money!='NA'& primintdata$nomotiv!='NA'& primintdata$days!='NA'& primintdata$noexp!='NA'& primintdata$negexp!='NA'& primintdata$rnum!='NA'& primintdata$anesrefuse!='NA' & primintdata$primint!='NA')

summary(primintdata)
primintdata$primint[primintdata$primint=='99']<-NA
summary(primintdata)

library(Amelia)

primint.out<-amelia(primintdata, m=5, noms=c("female", "anesrefuse", "race", "home", "emp", "lang", "alone", "webtv","nomotiv", "money", "negexp","noexp","primint"), ords=c("rnum", "child", "days"))

primintmean1<-primint.out$imputations[[1]]$primint
primintmean2<-primint.out$imputations[[2]]$primint
primintmean3<-primint.out$imputations[[3]]$primint
primintmean4<-primint.out$imputations[[4]]$primint
primintmean5<-primint.out$imputations[[5]]$primint


meanprimint<-(summary(primintmean1)[4]+summary(primintmean1)[4]+summary(primintmean1)[4]+summary(primintmean1)[4]+summary(primintmean1)[4])/5
meanprimint

summary(primintdata$primint)

#######INTUSE

#####intuse

intusedata<-data.frame(age=anes$der02, female=anes$aneswoman1, race=anes$nonwhite, educ=anes$aneseduc1, income=anes$anesincome1, home=anes$aneshome1, emp=anes$anesemp1, child=anes$children1, lang=anes$anesotherlanguage1, alone=anes$anesalone1, polint=anes$polinterest1, webtv=anes$aneswebtv1, money=anes$moneyonly1, nomotiv=anes$nonmotiv1, days=anes$days1, noexp=anes$noexpw1, negexp=anes$negexpw1, rnum=anes$rnum1, refuse=anes$anesrefuse, intuse=anes$intuse)


intusedata<-subset(intusedata, intusedata$age!='NA'& intusedata$female!='NA'& intusedata$race!='NA'& intusedata$educ!='NA'& intusedata$income!='NA'& intusedata$home!='NA'& intusedata$emp!='NA'& intusedata$child!='NA'& intusedata$lang!='NA'& intusedata$alone!='NA'& intusedata$polint!='NA'& intusedata$webtv!='NA'& intusedata$money!='NA'& intusedata$nomotiv!='NA'& intusedata$days!='NA'& intusedata$noexp!='NA'& intusedata$negexp!='NA'& intusedata$rnum!='NA'& intusedata$refuse!='NA' & intusedata$intuse!='NA')

summary(intusedata)
intusedata$intuse[intusedata$intuse=='99']<-NA
summary(intusedata)

library(Amelia)

intuse.out<-amelia(intusedata, m=5, noms=c("female", "refuse", "race", "home", "emp", "lang", "alone", "webtv","nomotiv", "money", "negexp","noexp"), ords=c("rnum", "child", "days", "intuse"))

intusemean1<-intuse.out$imputations[[1]]$intuse
intusemean2<-intuse.out$imputations[[2]]$intuse
intusemean3<-intuse.out$imputations[[3]]$intuse
intusemean4<-intuse.out$imputations[[4]]$intuse
intusemean5<-intuse.out$imputations[[5]]$intuse


meanintuse<-(summary(intusemean1)[4]+summary(intusemean1)[4]+summary(intusemean1)[4]+summary(intusemean1)[4]+summary(intusemean1)[4])/5
meanintuse

summary(intusedata$intuse)


#####papnews2

papnews2data<-data.frame(age=anes$der02, female=anes$aneswoman1, race=anes$nonwhite, educ=anes$aneseduc1, income=anes$anesincome1, home=anes$aneshome1, emp=anes$anesemp1, child=anes$children1, lang=anes$anesotherlanguage1, alone=anes$anesalone1, polint=anes$polinterest1, webtv=anes$aneswebtv1, money=anes$moneyonly1, nomotiv=anes$nonmotiv1, days=anes$days1, noexp=anes$noexpw1, negexp=anes$negexpw1, rnum=anes$rnum1, papnews2=anes$papnews2, refuse=anes$anesrefuse)

papnews2data<-subset(papnews2data, papnews2data$age!='NA'& papnews2data$female!='NA'& papnews2data$race!='NA'& papnews2data$educ!='NA'& papnews2data$income!='NA'& papnews2data$home!='NA'& papnews2data$emp!='NA'& papnews2data$child!='NA'& papnews2data$lang!='NA'& papnews2data$alone!='NA'& papnews2data$polint!='NA'& papnews2data$webtv!='NA'& papnews2data$money!='NA'& papnews2data$nomotiv!='NA'& papnews2data$days!='NA'& papnews2data$noexp!='NA'& papnews2data$negexp!='NA'& papnews2data$rnum!='NA'& papnews2data$refuse!='NA' & papnews2data$papnews2!='NA')

summary(papnews2data)
papnews2data$papnews2[papnews2data$papnews2=='99']<-NA
summary(papnews2data)

library(Amelia)

papnews2.out<-amelia(papnews2data, m=5, noms=c("female", "refuse", "race", "home", "emp", "lang", "alone", "webtv","nomotiv", "money", "negexp","noexp","papnews2"), ords=c("rnum", "child", "days"))

papnews2mean1<-papnews2.out$imputations[[1]]$papnews2
papnews2mean2<-papnews2.out$imputations[[2]]$papnews2
papnews2mean3<-papnews2.out$imputations[[3]]$papnews2
papnews2mean4<-papnews2.out$imputations[[4]]$papnews2
papnews2mean5<-papnews2.out$imputations[[5]]$papnews2


meanpapnews2<-(summary(papnews2mean1)[4]+summary(papnews2mean1)[4]+summary(papnews2mean1)[4]+summary(papnews2mean1)[4]+summary(papnews2mean1)[4])/5
meanpapnews2

summary(papnews2data$papnews2)

#####global

globaldata<-data.frame(age=anes$der02, female=anes$aneswoman1, race=anes$nonwhite, educ=anes$aneseduc1, income=anes$anesincome1, home=anes$aneshome1, emp=anes$anesemp1, child=anes$children1, lang=anes$anesotherlanguage1, alone=anes$anesalone1, polint=anes$polinterest1, webtv=anes$aneswebtv1, money=anes$moneyonly1, nomotiv=anes$nonmotiv1, days=anes$days1, noexp=anes$noexpw1, negexp=anes$negexpw1, rnum=anes$rnum1,refuse=anes$anesrefuse, global=anes$global)

globaldata<-subset(globaldata, globaldata$age!='NA'& globaldata$female!='NA'& globaldata$race!='NA'& globaldata$educ!='NA'& globaldata$income!='NA'& globaldata$home!='NA'& globaldata$emp!='NA'& globaldata$child!='NA'& globaldata$lang!='NA'& globaldata$alone!='NA'& globaldata$polint!='NA'& globaldata$webtv!='NA'& globaldata$money!='NA'& globaldata$nomotiv!='NA'& globaldata$days!='NA'& globaldata$noexp!='NA'& globaldata$negexp!='NA'& globaldata$rnum!='NA'& globaldata$refuse!='NA' & globaldata$global!='NA')

summary(globaldata)
globaldata$global[globaldata$global=='99']<-NA
summary(globaldata)

library(Amelia)

global.out<-amelia(globaldata, m=5, noms=c("female", "refuse", "race", "home", "emp", "lang", "alone", "webtv","nomotiv", "money", "negexp","noexp","global"), ords=c("rnum", "child", "days"))

globalmean1<-global.out$imputations[[1]]$global
globalmean2<-global.out$imputations[[2]]$global
globalmean3<-global.out$imputations[[3]]$global
globalmean4<-global.out$imputations[[4]]$global
globalmean5<-global.out$imputations[[5]]$global


meanglobal<-(summary(globalmean1)[4]+summary(globalmean1)[4]+summary(globalmean1)[4]+summary(globalmean1)[4]+summary(globalmean1)[4])/5
meanglobal

summary(globaldata$global)


##############################GSS Analysis###################################


gss<-(GSS.dta)

gssattrition<-c()
gssattrition[gss$panstat_3=='selected, eligible, but not reinterviewed']<-1
gssattrition[gss$panstat_3=='attrited']<-1
gssattrition[gss$panstat_3=='selected, eligible, and reinterviewed']<-0
gssattrition[gss$panstat_2=='selected, but not eligible and not reinterviewd']<-NA
gssattrition[gss$panstat_2=='selected, but not eligible and not reinterviewd because r was in institution']<-NA
gssattrition[gss$panstat_2=='selected, but not eligible and not reinterviewd because r was in institution']<-NA
gssattrition[gss$panstat_2=='selected, but not eligible and not reinterviewd because r was deceased']<-NA

summary(gss$coop_1)
coop<-c()
coop[gss$coop_1=='friendly,interested']<-1
coop[gss$coop_1=='cooperative']<-1
coop[gss$coop_1=='restless,impatient']<-0
coop[gss$coop_1=='hostile']<-0
summary(coop)
table(coop)


gssfulltime<-c()
gssfulltime[gss$wrkstat_1=='working fulltime']<-1
gssfulltime[gss$wrkstat_1=='working parttime']<-1
gssfulltime[is.na(gssfulltime)]<-0

gssforeign<-c()
gssforeign[gss$born_1=='yes']<-0
gssforeign[gss$born_1=='no']<-1
table(gssforeign)
table(gss$born_1)

gsshome<-c()
table(gss$dwelown_1)
gsshome[gss$dwelown_1=='own or is buying']<-1
gsshome[gss$dwelown_1=='pays rent']<-0
gsshome[gss$dwelown_1=='other']<-0
table(gsshome)

table(gss$race_1)
gssrace<-c()
gssrace[gss$race_1=='2']<-1
gssrace[gss$race_1=='1']<-0
gssrace[gss$race_1=='3']<-1
table(gssrace)

table(gss$income06_1)
gssincome<-c()
gssincome[gss$income06_1=='under $1 000']<-1
gssincome[gss$income06_1=='$1 000 to 2 999']<-1
gssincome[gss$income06_1=='$3 000 to 3 999']<-1
gssincome[gss$income06_1=='$4 000 to 4 999']<-1
gssincome[gss$income06_1=='$5 000 to 5 999']<-2
gssincome[gss$income06_1=='$6 000 to 6 999']<-2
gssincome[gss$income06_1=='$7 000 to 7 999']<-2
gssincome[gss$income06_1=='$8 000 to 9 999']<-3
gssincome[gss$income06_1=='$10000 to 12499']<-4
gssincome[gss$income06_1=='$12500 to 14999']<-5
gssincome[gss$income06_1=='$15000 to 17499']<-6
gssincome[gss$income06_1=='$17500 to 19999']<-6
gssincome[gss$income06_1=='$20000 to 22499']<-7
gssincome[gss$income06_1=='$22500 to 24999']<-7
gssincome[gss$income06_1=='$25000 to 29999']<-8
gssincome[gss$income06_1=='$30000 to 34999']<-9
gssincome[gss$income06_1=='$35000 to 39999']<-10
gssincome[gss$income06_1=='$50000 to 59999']<-12
gssincome[gss$income06_1=='$40000 to 49999']<-11
gssincome[gss$income06_1=='$60000 to 74999']<-13
gssincome[gss$income06_1=='$75000 to $89999']<-14
gssincome[gss$income06_1=='$90000 to $109999']<-15
gssincome[gss$income06_1=='$110000 to $129999']<-16
gssincome[gss$income06_1=='$130000 to $149999']<-17
gssincome[gss$income06_1=='$150000 or over']<-18
summary(gssincome)
table(gssincome)

table(gss$degree_1)

gsseduc2<-c()
gsseduc2[gss$degree_1=='lt high school']<-1
gsseduc2[gss$degree_1=='high school']<-2
gsseduc2[gss$degree_1=='junior college']<-3
gsseduc2[gss$degree_1=='high school'& gss$educ_1>12]<-3
gsseduc2[gss$degree_1=='bachelor']<-4
gsseduc2[gss$degree_1=='graduate']<-5
table(gsseduc2)
summary(gss$educ2[gss$gssattrition1==0])
summary(gss$educ2[gss$gssattrition1==1])

summary(gss$gsseducation[gss$gssattrition1==0])
summary(gss$gsseducation[gss$gssattrition1==1])


gssage<-gss$age_1

table(gss$hompop_1)
gssalone<-c()
gssalone[gss$hompop_1=='1']<-1
gssalone[is.na(gssalone)]<-0
table(gssalone)

gssexperience<-gss$intyrs_1

gsslength<-gss$lngthinv_1

table(gss$sex_1)
gsssex<-c()
gsssex[gss$sex_1=='1']<-0
gsssex[gss$sex_1=='2']<-1
table(gsssex)


table(gss$intsex_1)
gssintwoman<-c()
gssintwoman[gss$intsex_1=='female']<-1
gssintwoman[gss$intsex_1=='male']<-0
table(gssintwoman)


gsssexsame<-c()
gsssexsame[gsssex=='0' & gssintwoman=='0']<-1 #male+male
gsssexsame[gsssex=='1' & gssintwoman=='1']<-1 #female+female
gsssexsame[gsssex=='0'  & gssintwoman=='1']<-0 #male+female
gsssexsame[gsssex=='1' & gssintwoman=='0']<-0 #female+male
summary(gsssexsame)

gssvote04<-c()
gssvote04[gss$vote04_1=='voted']<-1
gssvote04[is.na(gssvote04)]<-0
table(gssvote04)
table(gss$vote04_1)


gssinternet<-c()
gssinternet[gss$intrhome_1=='yes']<-1
gssinternet[gss$intrhome_1=='no']<-0
table(gssinternet)
table(gss$intrhome_1)

gchildren<-gss$babies_1 
summary(gchildren)

gssrefuse<-c()
gssrefuse[is.na(gss$happy_1)]<-1
gssrefuse[is.na(gss$attend_1)]<-1
gssrefuse[is.na(gss$polviews_1)]<-1
gssrefuse[is.na(gss$class_1)]<-1
gssrefuse[is.na(gss$relig_1)]<-1
gssrefuse[is.na(gss$vote04_1)]<-1
gssrefuse[is.na(gss$partyid_1)]<-1
gssrefuse[is.na(gss$satfin_1)]<-1
gssrefuse[is.na(gss$finrela_1)]<-1
gssrefuse[is.na(gss$bible_1)]<-1
gssrefuse[is.na(gssrefuse)]<-0
table(gssrefuse)

gss$gssattrition1<-gssattrition
gss$gssage1<-gssage
gss$gsssex1<-gsssex
gss$gssrace1<-gssrace
gss$gssincome1<-gssincome
gss$gsshome1<-gsshome
gss$gssfulltime1<-gssfulltime
gss$gchildren1<-gchildren
gss$gssforeign1<-gssforeign
gss$gssvote041<-gssvote04
gss$gssalone1<-gssalone
gss$gssexperience1<-gssexperience
gss$gssintwoman1<-gssintwoman
gss$gsssexsame1<-gsssexsame
gss$coop1<-coop
gss$internet1<-gssinternet
gss$educ2<-gsseduc2
gss$gssrefuse<-gssrefuse

gsslogitmin<- zelig(gssattrition1~gssage1+gsssex1+gssrace1+gssfulltime1+educ2+gssincome1+gsshome1+gchildren1+gssforeign1+gssalone1, model="logit", data=gss)
summary(gsslogitmin)
logLik(gsslogitmin)

gsslogitfull<- zelig(gssattrition1~gssage1+gsssex1+gssrace1+gssfulltime1+educ2+gssincome1+gsshome1+gchildren1+gssforeign1+gssvote041+gssalone1+gssexperience1+gssintwoman1+coop1+gssrefuse, model="logit", data=gss)
summary(gsslogitfull)
logLik(gsslogitfull)

###ALl First differences

######age
quantile(gss$gssage1, .05, na.rm = TRUE)
quantile(gss$gssage1, .95, na.rm = TRUE)
x.high<- setx(gsslogitfull, gssage1=79)
x.low <- setx(gsslogitfull, gssage1=22)
exp.out1<- sim(gsslogitfull, x1 = x.high, x = x.low) 
summary(exp.out1)
######age
x.high<- setx(gsslogitfull, gsssex1=1)
x.low <- setx(gsslogitfull, gsssex1=0)
exp.out2<- sim(gsslogitfull, x1 = x.high, x = x.low) 
summary(exp.out2)
######race
x.high<- setx(gsslogitfull, gssrace1=1)
x.low <- setx(gsslogitfull, gssrace1=0)
exp.out3<- sim(gsslogitfull, x1 = x.high, x = x.low) 
summary(exp.out3)
######work
x.high<- setx(gsslogitfull, gssfulltime1=1)
x.low <- setx(gsslogitfull, gssfulltime1=0)
exp.out4<- sim(gsslogitfull, x1 = x.high, x = x.low) 
summary(exp.out4)
######educ2
quantile(gss$educ2, .05, na.rm = TRUE)
quantile(gss$educ2, .95, na.rm = TRUE)
x.high<- setx(gsslogitfull, educ2=5)
x.low <- setx(gsslogitfull, educ2=1)
exp.out5<- sim(gsslogitfull, x1 = x.high, x = x.low) 
summary(exp.out5)
######educ2
quantile(gss$gssincome1, .05, na.rm = TRUE)
quantile(gss$gssincome1, .95, na.rm = TRUE)
x.high<- setx(gsslogitfull, gssincome1=18)
x.low <- setx(gsslogitfull, gssincome1=2)
exp.out6<- sim(gsslogitfull, x1 = x.high, x = x.low) 
summary(exp.out6)
######work
x.high<- setx(gsslogitfull, gsshome1=1)
x.low <- setx(gsslogitfull, gsshome1=0)
exp.out7<- sim(gsslogitfull, x1 = x.high, x = x.low) 
summary(exp.out7)
######children
quantile(gss$gchildren1, .05, na.rm = TRUE)
quantile(gss$gchildren1, .95, na.rm = TRUE)
x.high<- setx(gsslogitfull, gchildren1=1)
x.low <- setx(gsslogitfull, gchildren1=0)
exp.out8<- sim(gsslogitfull, x1 = x.high, x = x.low) 
summary(exp.out8)
######foreignborn
x.high<- setx(gsslogitfull, gssforeign1=1)
x.low <- setx(gsslogitfull, gssforeign1=0)
exp.out9<- sim(gsslogitfull, x1 = x.high, x = x.low) 
summary(exp.out9)
######foreignborn
x.high<- setx(gsslogitfull, gssvote041=1)
x.low <- setx(gsslogitfull, gssvote041=0)
exp.out10<- sim(gsslogitfull, x1 = x.high, x = x.low) 
summary(exp.out10)
######gssalone1
x.high<- setx(gsslogitfull, gssalone1=1)
x.low <- setx(gsslogitfull, gssalone1=0)
exp.out11<- sim(gsslogitfull, x1 = x.high, x = x.low) 
summary(exp.out11)
######experience
quantile(gss$gssexperience1, .05, na.rm = TRUE)
quantile(gss$gssexperience1, .95, na.rm = TRUE)
x.high<- setx(gsslogitfull, gssexperience1=11.8)
x.low <- setx(gsslogitfull, gssexperience1=0.4)
exp.out18<- sim(gsslogitfull, x1 = x.high, x = x.low) 
summary(exp.out18)
######female interviewer
x.high<- setx(gsslogitfull, gssintwoman1=1)
x.low <- setx(gsslogitfull, gssintwoman1=0)
exp.out12<- sim(gsslogitfull, x1 = x.high, x = x.low) 
summary(exp.out12)
######same sex
#x.high<- setx(gsslogitfull, gsssexsame1=1)
#x.low <- setx(gsslogitfull, gsssexsame1=0)
#exp.out13<- sim(gsslogitfull, x1 = x.high, x = x.low) 
#summary(exp.out13)
######coop1
x.high<- setx(gsslogitfull, coop1=1)
x.low <- setx(gsslogitfull, coop1=0)
exp.out14<- sim(gsslogitfull, x1 = x.high, x = x.low) 
summary(exp.out14)
######gssrefuse
x.high<- setx(gsslogitfull, gssrefuse=1)
x.low <- setx(gsslogitfull, gssrefuse=0)
exp.out15<- sim(gsslogitfull, x1 = x.high, x = x.low) 
summary(exp.out15)
